package com.sun.decrypt;

import com.sun.decrypt.utils.MessageDigestUtils;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class MACTest {
    private static final String UTF8 = StandardCharsets.UTF_8.name();

    @Test
    public void jdkMac(){
        String str = "哈啊哈";
        String md5 = "HmacMD5";
        String key = "456";
        //f5afdebaa5e372ae42b86652cde91bd1
        String hexMd5 = MessageDigestUtils.doDigest(str, key, md5);
        System.out.println(hexMd5);
        String sha256 = "HmacSHA256";
        String hexsha256 = MessageDigestUtils.doDigest(str, key, sha256);
        //d47965a6f63348812c18c7c0d8f76dec4b34a7a4a6f60641c162499539cfcc56
        System.out.println(hexsha256);
        String sha512 = "HmacSHA512";
        String hexsha512 = MessageDigestUtils.doDigest(str, key, sha512);
        //09f99033b1d02211c5c56cfbb478ce32c4c0d057e7d1efe3b3c4176c0a436244bb6c0509d3e4ceb85c9dab44882219f3b571dce3354c147cbfcc0d9b2af39960
        System.out.println(hexsha512);

    }

    @Test
    public void codecMac() throws UnsupportedEncodingException {
        String str = "哈啊哈";
        String key = "456";
        String md5 = new HmacUtils(HmacAlgorithms.HMAC_MD5, key.getBytes(UTF8)).hmacHex(str.getBytes(UTF8));
        String sha256 = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key.getBytes(UTF8)).hmacHex(str.getBytes(UTF8));
        String sha512 = new HmacUtils(HmacAlgorithms.HMAC_SHA_512, key.getBytes(UTF8)).hmacHex(str.getBytes(UTF8));
        /**
         * f5afdebaa5e372ae42b86652cde91bd1
         * d47965a6f63348812c18c7c0d8f76dec4b34a7a4a6f60641c162499539cfcc56
         * 09f99033b1d02211c5c56cfbb478ce32c4c0d057e7d1efe3b3c4176c0a436244bb6c0509d3e4ceb85c9dab44882219f3b571dce3354c147cbfcc0d9b2af39960
         */
        System.out.println(md5);
        System.out.println(sha256);
        System.out.println(sha512);
    }
}
